<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { customsAdd } from '@/api/common'
import { customsClearanceCompanyType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  write?.getRlus().value?.validate(async (valid, fields) => {
    if (valid) {
      const data = {
        name: write?.getFormData()?.name,
        contact: write?.getFormData()?.contact,
        placestart_id: write?.getFormData()?.contact,
        mobile: write?.getFormData()?.mobile,
        address: write?.getFormData()?.address,
        fax: write?.getFormData()?.fax,
        credit: write?.getFormData()?.credit,
        email: write?.getFormData()?.email,
        bank_account: write?.getFormData()?.bank_account,
        remark: write?.getFormData()?.remark,
        transport: write?.getFormData()?.wayArr
      } as customsClearanceCompanyType
      // data.buytime = '' + parseInt(data.buytime) / 1000
      console.log('---客户新增参数---', data)
      const res = await customsAdd(data)
      if (res) {
        emitter.emit('getList', 'add')
        push('/sysParameter/CustomsClearanceFormCompany')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<template>
  <ContentDetailWrap
    :title="t('exampleDemo.add')"
    @back="push('/sysParameter/CustomsClearanceFormCompany')"
  >
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
